package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.service.TbGoodsService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class TbGoodsServiceImpl implements TbGoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public void insertGoodInfo(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, List<TbItem> itemList, String nickName) {
//        tbGoodsMapper.insertByGoodID(tbGoods);
//           Long i = tbGoods.getId();
//           tbGoodsDesc.setGoodsId(i);
//           tbGoodsDescMapper.insertSelective(tbGoodsDesc);
//           tbItem.setGoodsId(i);
//           tbItemMapper.insertSelective(tbItem);
        //增加goods,返回主键
        tbGoodsMapper.insertByGoodID(tbGoods);
        Long goodsId = tbGoods.getId();
        tbGoodsDesc.setGoodsId(goodsId);
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        String sellerId = tbGoods.getSellerId();
        tbItemMapper.insertByList(goodsId,sellerId,itemList,nickName);
    }


    //    @Override
//    @Log
//    public void insertGoodInfo(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, TbItem tbItem) {
//        // 增加 返回主键
//        try {
//            tbGoodsMapper.insertByGoodID(tbGoods);
//            Long i = tbGoods.getId();
//            tbGoodsDesc.setGoodsId(i);
//            tbGoodsDescMapper.insertSelective(tbGoodsDesc);
//            tbItem.setGoodsId(i);
//            tbItemMapper.insertSelective(tbItem);
//        }catch (Exception e){
//        }
//
//    }

    @Override
    public List<TbGoods> getGoodsInfo(TbGoods tbGoods) {
        List<TbGoods> list = tbGoodsMapper.selectByTbItemCat(tbGoods);
        return list;
    }

    @Override
    public Result getGoodList(String sellerId, TbGoods goods) {
        String auditStatus = goods.getAuditStatus();
        String goodsName = goods.getGoodsName();
        List<TbGoods> tbGoods = tbGoodsMapper.selectByTbItemCat(goods);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbGoods);
    }

    @Override
    public Result updateBatch(String[] ids, String s) {
        tbGoodsMapper.updateBatch(ids, s);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    public Result deleteBatch(String[] ids) {
        tbGoodsMapper.updateBatchDelete(ids);
        return Result.build(ConstantEnum.SUCCESS);
    }


    // --------       查询详情        -----------

    @Override
    public TbGoods selectByGoodsId(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public List<TbGoods> getGoodsInfos() {
        List<TbGoods> list = tbGoodsMapper.selectByExample(null);
        return list;
    }

    @Override
    public int updateStatus(String[] ids, String s) {
        int i = tbGoodsMapper.updateStatus(ids,s);
        return i;
    }
}
